package com.easylinkin.linkappapi.safe.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.safe.dto.InspectionTaskDTO;
import com.easylinkin.linkappapi.safe.entity.InspectionTask;
import com.easylinkin.linkappapi.safe.service.InspectionTaskService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 自检任务管理
 */
@RestController
@RequestMapping("inspectionTask")
@Api(tags = "自检任务管理")
public class InspectionTaskController {
  
  @Autowired
  private InspectionTaskService inspectionTaskService;
  @Autowired
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * @Description: 增加自检任务
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加自检任务")
  public RestMessage insert(@RequestBody InspectionTask inspectionTask){
    //todo 参数验证
    inspectionTaskService.insert(inspectionTask);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 删除自检任务（包含批量删除）
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("删除自检任务（包含批量删除）")
  public RestMessage delBatch(@RequestBody InspectionTask inspectionTask){
    Assert.notEmpty(inspectionTask.getIds(),"id不能为空");
    inspectionTaskService.removeByIds(inspectionTask.getIds());
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑自检任务
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑自检任务")
  public RestMessage updateById(@RequestBody InspectionTask inspectionTask){
    Assert.notNull(inspectionTask.getId(),"id不能为空");
    inspectionTaskService.updateOne(inspectionTask);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询自检任务详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询自检任务详情")
  public RestMessage findById(@PathVariable("id")Integer id) {
    Assert.notNull(id,"id不能为空");
    InspectionTaskDTO inspectionTaskDTO = inspectionTaskService.findById(id);
    return RestBuilders.successBuilder().data(inspectionTaskDTO).build();
  }

  /**
   * @Description: 根据条件，不分页查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，不分页查询")
  public RestMessage queryList(@RequestBody RequestModel<InspectionTaskDTO> requestModel,Integer type){
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    //加type查我的自检任务
    if (null != type){
      requestModel.getCustomQueryParams().setCreatorId(linkappUserContextProducer.getCurrent().getId());
      requestModel.getCustomQueryParams().setMyType(1);
    }
    IPage<InspectionTaskDTO> record = inspectionTaskService.queryList(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }
}

